package com.macro.mall.util;

import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.metadata.Sheet;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import java.io.*;
import java.util.List;

//import com.hkt.manager.platform.service.HktAdminFileService;

/**
 * @ClasssName: UploadServiceImpl
 * @description: TODO
 * @Auctior: limi
 * @Date: 2019/8/149:41
 **/
@Service
@Transactional
public class UploadServiceImpl {

    public List<Object> readExecl(String FilePath) throws Exception {
        if (!StringUtils.isEmpty(FilePath)) {
            FileInputStream fileInputStream = null;
            List<Object> read = null;
            try {
                File file = new File(FilePath);
                fileInputStream = new FileInputStream(file);
                read = readExecl(fileInputStream, new Sheet(1, 0));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                throw new Exception("读文件失败！！");
            } finally {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return read;
        }
        return null;
    }

    public List<Object> readExecl(String FilePath, Sheet sheet) {
        if (!StringUtils.isEmpty(FilePath)) {
            List<Object> read = null;
            FileInputStream fileInputStream = null;
            try {
                File file = new File(FilePath);
                fileInputStream = new FileInputStream(file);
                read = readExecl(fileInputStream, sheet);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } finally {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    public List<Object> readExecl(MultipartHttpServletRequest mreq) {
        List<Object> read = null;
        try {
            if (null == mreq.getInputStream()) {
                return null;
            }
            Sheet sheet = new Sheet(1, 0);
            read = readExecl(mreq.getInputStream(), sheet);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return read;
    }

    public List<Object> readExecl(MultipartHttpServletRequest mreq, Sheet sheet) {
        List<Object> read = null;
        try {
            if (null == mreq.getInputStream()) {
                return null;
            }
            if (null == sheet) {
                sheet = new Sheet(1, 0);
            }
            read = readExecl(mreq.getInputStream(), sheet);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return read;
    }

    public List<Object> readExecl(InputStream inputStream, Sheet sheet) {
        List<Object> read = null;
        if (null != inputStream) {
            read = EasyExcelFactory.read(new BufferedInputStream(inputStream), sheet);
        }
        return read;
    }

}
